<!-- 分出客户 选择顾问 -->
<template>
  <div class=''>
    <dt-modal-edit ref='info' title='请选择需分入客户的人员' :width='60' @ok='onOk'>
      <Table :columns="columns" :data="data" border height="400">
      </Table>
    </dt-modal-edit>
  </div>
</template>

<script>
  import customerMgr from '@/biz/customer/customer'
  export default {
    components: {},
    data() {
      return {
        columns: [
        {
          title: '选择',
          key: 'userId',
          width: 70,
          align: 'center',
          render: (h, params) => {
            let id = params.row.userId
            let flag = false
            if (this.currentChoose === id) {
              flag = true
            } else {
              flag = false
            }
            let self = this
            return h('div', [
              h('Radio', {
                props: {
                  value: flag,
                },
                on: {
                  'on-change': () => {
                    console.log(params.row, 'params.row')
                    this.currentChooseName = params.row.userName
                    console.log(id)
                    self.currentChoose = id
                  },
                },
              }),
            ])
          },
        },
        {
          title: '姓名',
          key: 'userName',
          minWidth: 120,
        },
         {
          title: '角色',
          key: 'roleStr',
          minWidth: 120,
        },
        {
          title: '手机号',
          key: 'phone',
          minWidth: 120,
        }],
        data: [],
        customerChoosedLength: 0,
        customerAllLength: 0,
        modal1: false,
        currentChoose: '',
        currentChooseName: '',
        operationType: 0, //分出类型
        customerIds: [], //选中的客户ID
        currentName: '',
        adviserVal: ''
      };
    },
    methods: {
      show(data) {
        this.currentChoose = ''
        this.customerIds = data.list
        this.currentName = data.currentName
        this.adviserVal = data.adviserVal
        this.operationType = data.operationType
        this.customerChoosedLength = data.customerChoosedLength
        this.customerAllLength = data.customerAllLength
        this.$refs.info.show()
        this.getAdviserList()
      },
      hide() {
        this.customerIds = []
        this.customerChoosedLength = 0
        this.$refs.info.hide()
      },
      // 分配确定
      onOk() {
        if (!this.currentChoose) {
          this.$Message.warning('请选择分给的人员。')
          return false
        }
        let numLen = this.operationType ? this.customerAllLength : this.customerChoosedLength
        this.$Modal.confirm({
          title: '分配客户确认',
          content: `<p>是否将${this.currentName}的${numLen}个客户分配给${this.currentChooseName}？</p>`,
          onOk: () => {
            let params = {
              userId: this.$dt.userInfo().userId,
              buildingId: this.$dt.buildInfo().housesId,
              newUserId: this.currentChoose,
              oldUserId: this.adviserVal,
              operationType: this.operationType, //操作类型：0部分，1全部
              adminRole: '1',
              onRecord: 1,
            }
            if (this.operationType == 0) {
              params.customerIds = this.customerIds
            }
            customerMgr.customerAssignment(params).then((res) => {
              this.$Message.success('分配成功。')
              this.$emit('onsuccess')
              this.hide()
            })
          }
        })
      },
      getAdviserList() {
        let params = {
          buildingId: Number(this.$dt.buildInfo().housesId),
          onlineType: 1,
          haveRole:3,
        }
        customerMgr.adviserList(params).then((res) => {
          this.data = [...res]
          let _index = this.data.findIndex((item) => {
            return item.userId == this.adviserVal
          })
          if (_index != -1) {
            this.data.splice(_index, 1)
          }
          this.data.forEach(item=>{
            let roleStr = []
            item.roleList&&item.roleList.length&&item.roleList.forEach(roleChild=>{
              roleStr.push(roleChild.roleName)
            })
            item.roleStr = roleStr.join(',')
          })
          
        })
      },
    }
  }
</script>
<style lang='scss' scoped>

</style>